package com.antdesignbackend.backend.form.animalDaily.service.impl;


import com.antdesignbackend.backend.activiti.constant.Constants;
import com.antdesignbackend.backend.activiti.processlist.entity.ProcessList;
import com.antdesignbackend.backend.activiti.processstay.entity.Processstay;
import com.antdesignbackend.backend.activiti.processstay.service.ProcessstayService;
import com.antdesignbackend.backend.activiti.reviewer.entity.Reviewer;
import com.antdesignbackend.backend.activiti.util.ActivitiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.antdesignbackend.backend.form.animalDaily.mapper.AnimalDailyMapper;
import com.antdesignbackend.backend.form.animalDaily.entity.AnimalDaily;
import com.antdesignbackend.backend.form.animalDaily.service.AnimalDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class AnimalDailyServiceImpl extends ServiceImpl<AnimalDailyMapper, AnimalDaily> implements AnimalDailyService {
    //表单标识
    private static final Integer Form = Constants.ANIMALDAILY;
    @Autowired
    AnimalDailyService animalDailyService;
    @Autowired
    ProcessstayService processstayService;
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void submit(AnimalDaily animalDaily) throws RuntimeException {
        if (animalDailyService.save(animalDaily)) {//保存成功
            approval(animalDaily);
        } else {
            throw new RuntimeException("提交失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void updateSubmit(AnimalDaily animalDaily) throws RuntimeException {
        if (animalDailyService.updateById(animalDaily)) {//保存成功
            approval(animalDaily);
        } else {
            throw new RuntimeException("提交失败！");
        }
    }

    public void approval(AnimalDaily animalDaily){
        //根据标识获得流程id
        Integer processId = ActivitiUtil.getProcessIdByForm(Form);
        if (processId == null)
            throw new RuntimeException("该流程不存在！");
        //获得下一个审批节点
        ProcessList processList = ActivitiUtil.getProcessList(processId,0);
        if (processList == null)
            throw new RuntimeException("审批不存在！");
        if (processList.getTypes().equals(Constants.SUCCESSIVELY)){ //依次审批
            //获得下一个审批人
            Reviewer reviewer = ActivitiUtil.getReviewerNext(processList.getId(),0);
            if (reviewer == null)
                throw new RuntimeException("审批人不存在！");
            //创建待审记录
            Processstay processstay = new Processstay();
            processstay.setProcessId(processId);
            processstay.setForm(Form);
            processstay.setFormId(animalDaily.getId());
            processstay.setProcesslistId(processList.getId());
            processstay.setTypes(processList.getTypes());
            processstay.setReviewerId(reviewer.getId());
            processstay.setUserId(reviewer.getReviewer());
            processstay.setLaunchId(animalDaily.getCreateBy());
            processstay.setLaunchTime(animalDaily.getCreateTime());
            processstayService.save(processstay);
        }else { //会签或或签
            //获得下一批审批人
            List<Reviewer> list = ActivitiUtil.getReviewer(processList.getId());
            if (list == null)
                throw new RuntimeException("审批人不存在！");
            for (Reviewer reviewer:list){
                //创建待审记录
                Processstay processstay = new Processstay();
                processstay.setProcessId(processId);
                processstay.setForm(Form);
                processstay.setFormId(animalDaily.getId());
                processstay.setProcesslistId(processList.getId());
                processstay.setTypes(processList.getTypes());
                processstay.setReviewerId(reviewer.getId());
                processstay.setUserId(reviewer.getReviewer());
                processstay.setLaunchId(animalDaily.getCreateBy());
                processstay.setLaunchTime(animalDaily.getCreateTime());
                processstayService.save(processstay);
            }
        }
    }
}
